﻿#region 代码引用

using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using Aspose.Words;
using Aspose.Words.Settings;
using Aspose.Words.Tables;

#endregion

namespace Utils.Aspose
{
    public class AsposeWord
    {
        private Document doc;
        private DocumentBuilder builder;

        public AsposeWord()
        {
            doc = new Document();
            builder = new DocumentBuilder(doc);
        }

        public AsposeWord(string docPath)
        {
            doc = new Document(docPath);
            builder = new DocumentBuilder(doc);
        }

        public List<string> GetBookMarks() 
            => doc.Range.Bookmarks.Cast<Bookmark>().Select(bm => bm.Name).ToList();

        public void CreateBookmark(string bookmark)
        {
            builder.StartBookmark(bookmark);
            builder.Writeln(bookmark);
            builder.EndBookmark(bookmark);
        }

        public void WriteInBookMark(string bookMarkName, string value)
            => doc.Range.Bookmarks[bookMarkName].Text = value;

        public void InsertTable(DataTable dt, string bookMarkName, bool isHeader)
        {
            builder.MoveToBookmark(bookMarkName);
            Table table = builder.StartTable();
            table.SetBorders(LineStyle.Outset, 1, Color.Black);
            table.SetBorders(LineStyle.Inset, 1, Color.Black);

            if (isHeader)
            {
                CreateTableHeader(table, dt.Columns.Cast<DataColumn>().Select(a => a.ColumnName).ToArray());
            }

            foreach (DataRow dr in dt.Rows)
            {
                foreach (DataColumn dc in dt.Columns)
                {
                    CreateCellWrite(dr[dc].ToString());
                }
                builder.EndRow();
            }
            builder.EndTable();
        }

        private void CreateTableHeader(Table table, string[] columns)
        {
            table.SetBorders(LineStyle.Outset, 1, Color.Black);
            table.SetBorders(LineStyle.Inset, 1, Color.Black);
            foreach (string col in columns)
            {
                CreateCellWrite(col);
            }
            builder.EndRow();
        }

        private void CreateCellWrite(string text)
        {
            builder.InsertCell();
            builder.Write(text);
        }

        public void InsertPageHeader(string headerText)
        {
            builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
            builder.Font.Color = Color.Red;
            builder.Write(headerText);
        }

        public void SaveToWord(string path) => SaveToWord(path, "");

        public void SaveToWord(string path, string password)
        {
            try
            {
                string pathSufix = Path.GetExtension(path);
                if (string.IsNullOrWhiteSpace(pathSufix)) throw new Exception("保存文件后缀错误,现只支持*.doc,*.docx后缀");
                SaveFormat sufix = pathSufix.Equals(".docx", StringComparison.OrdinalIgnoreCase)
                    ? SaveFormat.Docx
                    : SaveFormat.Doc;
                doc.ViewOptions.ViewType = ViewType.PageLayout;

                if (!string.IsNullOrWhiteSpace(password))
                    doc.Protect(ProtectionType.AllowOnlyFormFields, "MarsFreesoft");

                doc.Save(path, sufix);
            }
            catch (IOException)
            {
                throw new Exception("服务器端历史合同正在启动中...");
            }
        }
    }
}
